layui.use(['form', 'layer', 'upload', 'laydate', 'treeSelect', 'layedit', 'tableSelect', 'table'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var laydate = layui.laydate;
    var treeSelect = layui.treeSelect;//树形下拉框


    setTimeout(() => {
        treeSelect.render({
            elem: '#categoryName',
            data: '/admin/facilityCategory/treelist',
            type: 'get',
            placeholder: '默认添加首级分类',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function (data) {
                $("#categoryName").val(data.current.name);
                $("#categoryId").val(data.current.id);

            },
            success: function (data) { // 加载完成后的回调函数
                $("#categoryName").val(data.data[0].name);
                $("#categoryId").val(data.data[0].id);
            }
        });
        treeSelect.render({
            elem: '#storageName',
            data: '/admin/storage/treelist',
            type: 'get',
            placeholder: '默认添加首级分类',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function (data) {
                $("#storageId").val(data.current.storageId);
                $("#storageName").val(data.current.name);
            },
            success: function (data) { // 加载完成后的回调函数
                console.log(data)
                $("#storageName").val(data.data[0].name);
                $("#storageId").val(data.data[0].storageId);
            }
        });

        treeSelect.render({
            elem: '#materialStorage',
            data: '/admin/fileStorage/treelist',
            type: 'get',
            placeholder: '默认添加首级分类',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function (data) {
                $("#materialStorageId").val(data.current.storageId);
                $("#materialStorage").val(data.current.name);
            },
            success: function (data) { // 加载完成后的回调函数
                $("#materialStorage").val(data.data[0].name);
                $("#materialStorageId").val(data.data[0].storageId);
                form.render('select');
            }
        });
    },50)
    //
    var date = new Date();
    laydate.render({
        elem: '#buyDate'
        ,
        value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
    });

    let tempStatus = "0";
    //监听仪器是否可用开关
    form.on('switch(switchTest)', function(data){
        if (this.checked) {
            $("#status").val('0');
            tempStatus = "0";
            console.log(0);
        } else {
            $("#status").val('1');
            tempStatus = "1";
            console.log(1);
        }
    });


    form.on("submit(addFacility)", function (data) {
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        data.field.status = tempStatus;
        $.ajax({
            url: '/admin/facility/add',
            data: data.field,
            type: 'post',
            success: function (data) {
                if (data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }else {

                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error: function (d) {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    })

})